package lk.service.blogspringboot.service.impl;

import lk.service.blogspringboot.domain.ArticleInfo;
import lk.service.blogspringboot.domain.ArticleLikeDetail;
import lk.service.blogspringboot.mapper.ArticleInfoMapper;
import lk.service.blogspringboot.mapper.ArticleLikeDetailMapper;
import lk.service.blogspringboot.service.ArticleLikeDetailService;
import lk.service.blogspringboot.util.R;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

/**
 * 文章点赞详细信息表(ArticleLikeDetail)表服务实现类
 *
 * @author LK
 * @since 2024-08-22 10:33:40
 */
@Service("articleLikeDetailService")
public class ArticleLikeDetailServiceImpl implements ArticleLikeDetailService {
    @Resource
    private ArticleLikeDetailMapper articleLikeDetailMapper;

    @Resource
    private ArticleInfoMapper articleInfoMapper;

    /**
     * 通过ID查询单条数据
     *
     * @param id 主键
     * @return 实例对象
     */
    @Override
    public R queryById(Integer id) {
        return R.ok().setData(this.articleLikeDetailMapper.queryById(id));
    }

    /**
     * 全查询
     *
     * @param articleLikeDetail 筛选条件
     * @return 查询结果
     */
    @Override
    public R queryAll(ArticleLikeDetail articleLikeDetail) {
        return R.ok().setData(this.articleLikeDetailMapper.queryAll(articleLikeDetail));
    }

    /**
     * 新增数据
     *
     * @param articleLikeDetail 实例对象
     * @return 实例对象
     */
    @Override
    public R insert(ArticleLikeDetail articleLikeDetail) {
        // 新增点赞数据
        this.articleLikeDetailMapper.insert(articleLikeDetail);
        // 新增之后 对应的文章的点赞量字段数目+1
        ArticleInfo articleInfo = articleInfoMapper.queryById(articleLikeDetail.getArticleId());
        articleInfo.setLikes(articleInfo.getLikes() + 1);
        articleInfoMapper.update(articleInfo);
        return R.ok().setData(articleLikeDetail);
    }

    /**
     * 修改数据
     *
     * @param articleLikeDetail 实例对象
     * @return 实例对象
     */
    @Override
    public R update(ArticleLikeDetail articleLikeDetail) {
        this.articleLikeDetailMapper.update(articleLikeDetail);
        return R.ok().setData(this.queryById(articleLikeDetail.getId()));
    }

    /**
     * 通过主键删除数据
     *
     * @param id 主键
     * @return 是否成功
     */
    @Override
    public R deleteById(Integer id) {
        boolean del = this.articleLikeDetailMapper.deleteById(id) > 0;
        return R.ok().setData(del);
    }

    @Override
    public R queryIsLike(Integer articleId, String liker) {
        if (articleLikeDetailMapper.queryIsLike(articleId, liker)) {
            return R.ok().setData(true);
        }else {
            return R.fail().setData(false);
        }
    }

    /**
     *  删除用户点赞信息
     */
    @Override
    public R deleteByUserNameAndArticleId(Integer articleId, String liker) {
        if (articleLikeDetailMapper.deleteByUserNameAndArticleId(articleId, liker)) {
            // 删除之后 对应的文章的点赞量字段数目-1
            ArticleInfo articleInfo = articleInfoMapper.queryById(articleId);
            articleInfo.setLikes(articleInfo.getLikes() - 1);
            articleInfoMapper.update(articleInfo);
            return R.ok();
        }else {
            return R.fail();
        }
    }
}

